<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script>
    function send() {
        if("" == $("#mail").val()){
            confirm("请输入邮箱！")
            return;
        }
            var s = document.getElementById("s")
            timer = setInterval(function () {
                var number = parseInt(s.value);
                if(isNaN(number)){
                    $.post("http://www.godme.com/api/code/send/"+$("#mail").val(), function (data, status) {
                        if("success" == status){
                            alert("验证码已发送，请及时注册");
                        }
                    });
                    number = 60;
                }
                if(number < 2){
                    s.value = "获取验证码";
                    clearInterval(timer);
                }else{
                    s.value = (number-1)+"s";
                }
            }, 1000)
        };
        function register() {
            var password = $("#password").val();
            var repeat = $("#repeat").val();
            var code = $("#code").val()
            if(password === ""){
                confirm("密码不能为空")
            }else if (password != repeat){
                confirm("两次密码输入不一致")
            }else if(code == ""){
                confirm("请输入验证码")
            }else if(password === repeat){
                $.ajax("http://www.godme.com/api/user/register/"+$("#mail").val()+"/"+$("#password").val()+"/"+$("#code").val(),
                    {
                        type: 'post',
                        success: function (data, status) {
                           if(data){
                               window.location.href = "/static/login.html";
                           }else {
                               confirm("注册失败，请重新注册")
                           }
                        },
                        error: function (data, status) {
                            console.log(data.responseText)
                           confirm(data.responseText);
                        }
                    })
            }else{

            }
        };
</script>
<body>
    <div style="margin-left: 45%; margin-top: 10%">
        <form id="form" action="/api/user/register" method="post">
            <table>
                <caption>register</caption>
                <tr>
                    <td> <input type="email" id="mail" placeholder="email"></td>
                </tr>
                <tr>
                    <td><input id="password" type="text" placeholder="password"></td>
                </tr>
                <tr>
                    <td><input id="repeat" type="text" placeholder="repeat" ></td>
                </tr>
                <tr>
                    <td><input type="text" id="code" placeholder="code"></td>
                </tr>
                <tr>
                    <td><input type="button" value="register" onclick="register()" style="float: left">  <input onclick="send()" id="s" value="获取验证码" type="button" style="float:right;"></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>